<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
    <el-form-item label="机构名称" prop="institutionName">
      <el-input v-model="dataForm.institutionName" placeholder="机构名称" clearable></el-input>
    </el-form-item>
      <el-form-item label="机构简介" prop="institutionInfo">
        <el-input
          type="textarea"
          autosize
          v-model="dataForm.institutionInfo"
          placeholder="机构简介"
          clearable>
        </el-input>
      </el-form-item>
    <el-form-item label="招新范围" prop="objectRange">
      <el-input v-model="dataForm.objectRange" placeholder="招新范围" clearable></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
        v-model="dataForm.startTime"
        type="datetime"
        placeholder="开始时间"
        suffix-icon="el-icon-date"
        style="width: 200px">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
        v-model="dataForm.endTime"
        type="datetime"
        placeholder="结束时间"
        suffix-icon="el-icon-date"
        style="width: 200px">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="招新方式" prop="waysRegistration">
      <el-input
        type="textarea"
        autosize
        v-model="dataForm.waysRegistration"
        placeholder="招新方式"
        clearable>
      </el-input>
    </el-form-item>
    <el-form-item label="招新说明" prop="description">
      <el-input
        type="textarea"
        autosize
        v-model="dataForm.description"
        placeholder="招新说明"
        clearable>
      </el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="principal">
      <el-input v-model="dataForm.principal" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="contactInfo">
      <el-input v-model="dataForm.contactInfo" placeholder="联系方式"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isMobile } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          id: 0,
          institutionName: '',
          institutionInfo: '',
          objectRange: '',
          startTime: '',
          endTime: '',
          waysRegistration: '',
          description: '',
          principal: '',
          contactInfo: ''
        },
        dataRule: {
          institutionName: [
            { required: true, message: '机构名称不能为空', trigger: 'blur' }
          ],
          institutionInfo: [
            { required: true, message: '机构简介不能为空', trigger: 'blur' }
          ],
          objectRange: [
            { required: true, message: '招新范围不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          waysRegistration: [
            { required: true, message: '招新方式不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '招新说明不能为空', trigger: 'blur' }
          ],
          principal: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          contactInfo: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/plan/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.institutionName = data.RecruitmentPlan.institutionName
                this.dataForm.objectRange = data.RecruitmentPlan.objectRange
                this.dataForm.startTime = data.RecruitmentPlan.startTime
                this.dataForm.endTime = data.RecruitmentPlan.endTime
                this.dataForm.waysRegistration = data.RecruitmentPlan.waysRegistration
                this.dataForm.description = data.RecruitmentPlan.description
                this.dataForm.principal = data.RecruitmentPlan.principal
                this.dataForm.contactInfo = data.RecruitmentPlan.contactInfo
              }
            })
          }
        })
      },
      dateChangebirthday (val) {
        this.dataForm.period = val;
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/plan/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'institutionName': this.dataForm.institutionName,
                'institutionInfo': this.dataForm.institutionInfo,
                'objectRange': this.dataForm.objectRange,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'waysRegistration': this.dataForm.waysRegistration,
                'description': this.dataForm.description,
                'principal': this.dataForm.principal,
                'contactInfo': this.dataForm.contactInfo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
